import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Main.vue'
import Store from "../store/index"
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    redirect: to => {
      // 方法接收目标路由作为参数
      // return 重定向的字符串路径/路径对象
      return { path:"/home" }
    },
    component: Home,
    children:[
      {
        path:"/home",
        name:"home",
        component:()=>import("../views/home/index.vue")
      },{
        path: '/user',
        name: 'User',
        component: () => import('../views/User/index.vue')
      },{
        path: '/mall',
        name: 'Mall',
        component: () => import('../views/mall/index.vue')
      },{
        path: '/page1',
        name: 'PageOne',
        component: () => import('../views/other/pageOne.vue')
      },{
        path: '/page2',
        name: 'PageTwo',
        component: () => import('../views/other/pageTwo.vue')
      },
    ]
  },{
    path:"/login",
    name:"Login",
    component:()=>import("../views/login/index.vue")
  },{
    path:"/register",
    name:"Register",
    component:()=>import("../views/register/index.vue")
  }
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  console.log(Store.state.canlogin);
  if(!Store.state.canlogin){
    if (to.path === '/login'||to.path === '/register') {
      next();
    } else {
      next('/login');
    }
  }
  else{
    next()
  }
})
export default router
